package com.medicalfee.controller;

import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.Controller;

import com.medicalfee.dao.StuffInfo;
import com.medicalfee.dao.StuffInfoDAO;

public class GetStuffBySelectController implements Controller {
	private StuffInfoDAO stuffInfoDao;

	public void setStuffInfoDao(StuffInfoDAO stuffInfoDao) {
		this.stuffInfoDao = stuffInfoDao;
	}

	@Override
	public ModelAndView handleRequest(HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		// TODO Auto-generated method stub
		String office = request.getParameter("office");
		List result = stuffInfoDao
				.getStuffByHql("from StuffInfo where offices='" + office + "'");
		JSONArray json = new JSONArray();
		for (int i = 0; i < result.size(); i++) {
			JSONObject jobject = new JSONObject();
			StuffInfo stuff = (StuffInfo) result.get(i);
			jobject.put("text", stuff.getName());
			jobject.put("sid", stuff.getId());
			json.add(jobject);
		}
		response.setCharacterEncoding("UTF-8");
		response.getWriter().print(json.toString());
		return null;
	}

}
